import tkinter as tk
from tkinter import filedialog, messagebox
from .add_task import add_task_func
from tkcalendar import DateEntry

def create_card_dialog_func():
    window = tk.Tk()
    window.withdraw()

    dialog = tk.Toplevel(window)
    dialog.title("Create New Task")
    dialog.geometry("400x300")
    
    task_name_var = tk.StringVar()
    priority_var = tk.StringVar(value="High")
    due_date_var = tk.StringVar()
    image_path_var = tk.StringVar()

    task_name_label = tk.Label(dialog, text="Task Name:")
    task_name_label.pack(pady=5)
    task_name_entry = tk.Entry(dialog, textvariable=task_name_var)
    task_name_entry.pack(pady=5)
    
    priority_label = tk.Label(dialog, text="Priority:")
    priority_label.pack(pady=5)
    priority_options = ["High", "Medium", "Low"]
    priority_menu = tk.OptionMenu(dialog, priority_var, *priority_options)
    priority_menu.pack(pady=5)
    
    due_date_label = tk.Label(dialog, text="Due Date:")
    due_date_label.pack(pady=5)
    due_date_entry = DateEntry(dialog, textvariable=due_date_var, date_pattern='yyyy-mm-dd')
    due_date_entry.pack(pady=5)

    # 创建选择图片功能
    def select_image():
        try:
            filename = filedialog.askopenfilename(title="Select Image", filetypes=[("Image Files", "*.png;*.jpg;*.jpeg;*.gif")])
            if filename:
                image_path_var.set(filename)
        except Exception as e:
            messagebox.showerror("wrongly select picture", str(e))

    # 创建确认按钮
    confirm_button = tk.Button(dialog, text="Confirm", command=lambda: add_task_func(task_name_var.get(), priority_var.get(), due_data_var.get(), image_path_var.get(), dialog))
    confirm_button.pack(pady=10)

    # 创建选择图片按钮
    select_image_button = tk.Button(dialog, text="Select Image", command=select_image)
    select_image_button.pack(pady=5)

    return dialog
